package com.zshan.clinic.admin.service.config;

import com.zshan.clinic.common.enums.YesOrNoEnum;
import com.zshan.clinic.common.exception.BusinessFailException;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.common.util.page.PageUtils;
import com.zshan.clinic.common.util.string.StringUtil;
import com.zshan.clinic.database.dao.ClinicConfigMapper;
import com.zshan.clinic.database.model.ClinicConfig;
import com.zshan.clinic.database.model.ClinicConfigExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 系统常量
 */
@Service
@Slf4j
public class ClinicConfigService {


    @Autowired
    private ClinicConfigMapper clinicConfigMapper;


    /**
     * 分页查询诊所配置
     * @param clinicId 配置名（可选条件）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<ClinicConfig> getClinicConfigListByCondition(Long clinicId, Integer pageNo, Integer pageSize) {
        ClinicConfigExample example = new ClinicConfigExample();
        ClinicConfigExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isBlank(clinicId)) {
            criteria.andClinicIdEqualTo(clinicId);
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = clinicConfigMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("create_time desc");
        List<ClinicConfig> list = clinicConfigMapper.selectByExample(example);
        return new PageInfo<>(totalCount, list);
    }

    /**
     * 添加诊所配置
     * @param clinicConfig 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addClinicConfig(ClinicConfig clinicConfig) {
        validateClinicConfig(clinicConfig);
        clinicConfig.setIsDelete(YesOrNoEnum.NO.getValue());
        clinicConfig.setIsEnabled(YesOrNoEnum.YES.getValue());
        clinicConfig.setCreateTime(new Date());
        clinicConfigMapper.insertSelective(clinicConfig);
    }

    /**
     * 校验诊所配置参数
     * @param clinicConfig 实体
     */
    public void validateClinicConfig(ClinicConfig clinicConfig) {

    }

    /**
     * 根据ID查询诊所配置
     * @param clinicId 主键
     * @return 诊所配置记录
     */
    public ClinicConfig getClinicConfigById(Long clinicId) {
        if (clinicId == null) {
            return null;
        }
        ClinicConfig clinicConfig = clinicConfigMapper.selectByPrimaryKey(clinicId);
        if (clinicConfig == null || YesOrNoEnum.YES.getValue().equals(clinicConfig.getIsDelete())) {
            return null;
        }
        return clinicConfig;
    }

    /**
     * 修改诊所配置
     * @param clinicConfig 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateClinicConfig(ClinicConfig clinicConfig) {
        validateClinicConfig(clinicConfig);
        ClinicConfig dbClinicConfig = getClinicConfigById(clinicConfig.getClinicId());
        if (dbClinicConfig == null) {
            throw new BusinessFailException("诊所配置不存在");
        }
        clinicConfig.setUpdateTime(new Date());
        clinicConfigMapper.updateByPrimaryKeySelective(clinicConfig);
    }

    /**
     * 删除诊所配置（逻辑删除）
     * @param clinicConfig 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteClinicConfig(ClinicConfig clinicConfig) {
        ClinicConfig dbClinicConfig = getClinicConfigById(clinicConfig.getClinicId());
        if (dbClinicConfig == null) {
            throw new BusinessFailException("诊所配置不存在");
        }
        dbClinicConfig.setIsDelete(YesOrNoEnum.YES.getValue());
        dbClinicConfig.setUpdateUserId(clinicConfig.getUpdateUserId());
        dbClinicConfig.setUpdateBy(clinicConfig.getUpdateBy());
        dbClinicConfig.setUpdateTime(new Date());
        clinicConfigMapper.updateByPrimaryKeySelective(dbClinicConfig);
    }

}
